package org.zfes.snowier.cloud.zbss.widget.act.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActQuestionDifficultyService;
import org.zfes.snowier.cloud.zbss.widget.act.CacheNameCollection;
import org.zfes.snowier.cloud.zbss.widget.act.dao.ActQuestionDifficultyMapper;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActQuestionDifficulty;
import java.util.stream.Stream;
@Service
public class ActQuestionDifficultyServiceImpl implements IActQuestionDifficultyService{
 	@Autowired
	private ActQuestionDifficultyMapper actQuestionDifficultyMapper;
	
 	@CacheEvict(value=CacheNameCollection.questionDifficultyComboboVo,allEntries=true)
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createActQuestionDifficulty(ActQuestionDifficulty actQuestionDifficulty) {
		ZBeanUtil.setCreateInfo(actQuestionDifficulty);
		ZBeanUtil.validateBean(actQuestionDifficulty);
		actQuestionDifficultyMapper.insertSelective(actQuestionDifficulty);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActQuestionDifficulty> loadActQuestionDifficultyById(Long id) {
		if(id!=null){
			return Optional.ofNullable(actQuestionDifficultyMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@CacheEvict(value=CacheNameCollection.questionDifficultyComboboVo,allEntries=true)
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateActQuestionDifficulty(ActQuestionDifficulty actQuestionDifficulty) {
		ZBeanUtil.setUpdatedInfo(actQuestionDifficulty);
		ZBeanUtil.validateBean(actQuestionDifficulty);
		actQuestionDifficultyMapper.updateByPrimaryKeySelective(actQuestionDifficulty);
	}
	
	@CacheEvict(value=CacheNameCollection.questionDifficultyComboboVo,allEntries=true)
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteActQuestionDifficulty(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		actQuestionDifficultyMapper.deleteByPrimaryKey(id);
	}
	@CacheEvict(value=CacheNameCollection.questionDifficultyComboboVo,allEntries=true)
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setActQuestionDifficultyStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		actQuestionDifficultyMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadActQuestionDifficultyDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(actQuestionDifficultyMapper.selectListCount(pm), actQuestionDifficultyMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Cacheable(value=CacheNameCollection.questionDifficultyComboboVo, key="questionDifficultyComboboVo")
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadComboboVo(boolean firstEmpty) {
		List<ComboboVo> voList=actQuestionDifficultyMapper.selectComboboVo();
		if(firstEmpty) {
			voList.add(0, ComboboVo.newEmptyFirst());
		}
		return voList;
	}
	
}